<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <link rel="stylesheet" href="./highlight/styles/agate.css"> 
    <script src="./highlight/highlight.pack.js"></script> 
    <script > hljs.initHighlightingOnLoad();</script> 
    <script src="./js/my-pre.js"></script>
 
   
</head>
<body>

    <div id="app">
        <h1 >自定义事件</h1>
        <h3>子组件用$emit来触发事件,父组件用$on来监听子组件的事件,父组件也可在子组件的自定义标签上使用v-on来监听子组件触发的自定义事件.</h3>
        <hr>
        <p>总数:{{total}}</p>
        <my-com @increase="handleGetTotal" @reduce="handleGetTotal"></my-com>
        <my-pre :init-string="string0"></my-pre>

        <h3>在组件上实现原生事件</h3>
        <my-com @click.native="handleClick(this)"></my-com>
        <h3>使用v-model来实现子到父的传递,$.emit('input','你要传递的数据')</h3>
        <p>总数:{{total1}}</p>
        <my-com2 v-model="total1"></my-com2>
        <my-pre :init-string="string1"></my-pre>

        <hr><br>
        <h3>v-model还可以创建自定义的表单输入组件,进行数据双向绑定</h3>

        <p>总数:{{total}}</p>
        <my-com3 v-model="total"></my-com3>
        <br>
        <button @click="handleReduce">其他地方来的数据,执行-1的操作,同时把改变的值,传到组件中</button>

       
       <my-pre :init-string="string2">

       </my-pre>

       <!-- <textarea type="text" value="" @keyup ="test()"></textarea> -->



    </div>
   
    <script>
        // function conertcode(data){
        //     return data.replace(/</g,'&lt;').replace(/>/g,'&gt;')
        // }

        function hereDoc(f) {　
                    return f.toString().replace(/^[^\/]+\/\*!?\s?/, '').replace(/\*\/[^\/]+$/, '');
                };
        
 

        //在html中展示html或者js的代码块的组件
        Vue.component('my-pre2',{
            props:{
                initString:{
                    type:String,
                    default:''
                }
            },
            template:'\
                    <div>\
                        <pre>\
                               <code class="javascript hljs" style="font-size:16px;width:1000px;font-family: Courier, monospace">{{transdata}}\
                                </code>\
                        </pre>\
                    </div>',
            data(){
                return {
                        transdata:this.initString
                }
            },
            // methods:{
            //     transdatas(){
            //         var area = document.getElementById('texts')
            //         addEventListener('keyup', function() {
            //             var code = document.querySelector('code');
            //             // code.innerHTML = area.value.replace(/</g,'&lt;').replace(/>/g,'&gt;');
            //             code.innerHTML = string.replace(/</g,'&lt;').replace(/>/g,'&gt;')
            //             // console.log(code.innerHTML);
            //             hljs.highlightBlock(code);
                       
            //         })
                    
            //     }
            // }
         
        });
        

                
        Vue.component('my-com3',{
            props:['value'],
            template:'<div><input :value="value" @input="updateValue" id="inp"><label for ="inp">这个是组件</label></div>',
            methods:{
                updateValue(event){
                    this.$emit('input',event.target.value);
                }
            }
        })
        

 


        Vue.component('my-com',{
            prorps:[],
            
            template:'\
            <div>\
            <button @click="handleIncrease">+1</button>\
            <button @click="handleReduce">-1</button>\
            </div>',
            data(){
                return {
                    counter:0
                }
            },
            methods:{
                handleIncrease(){
                    this.counter++;
                    this.$emit('increase',this.counter); //用$emit方法向父组件的increase自定义事件传递数据
                },
                handleReduce(){
                    this.counter--
                    this.$emit('reduce',this.counter);
                }
            }
        })

        Vue.component('my-com2',{
            template:'<button @click="handleClick">+1</button>',
            data(){
                return {
                    count:0
                }
            },
            methods:{
                handleClick(){
                    this.count++;
                    this.$emit('input',this.count)
                }
            }

        });

        var app = new Vue({
            el:'#app',
            data:{
                total:0,
                total1:0,
            string0: hereDoc(function(){/*
                <p>总数:{{total}}</p>
        <my-com @increase="handleGetTotal" @reduce="handleGetTotal"></my-com> //在组件上定义了2个自定义事件,在实例中
        Vue.component('my-com',{
            prorps:[],
            
            template:'\
            <div>\
            <button @click="handleIncrease">+1</button>\
            <button @click="handleReduce">-1</button>\
            </div>',
            data(){
                return {
                    counter:0
                }
            },
            methods:{
                handleIncrease(){
                    this.counter++;
                    this.$emit('increase',this.counter); //用$emit方法向父组件的increase自定义事件传递数据
                },
                handleReduce(){
                    this.counter--
                    this.$emit('reduce',this.counter);//用$emit方法向父组件的increase自定义事件传递数据
                }
            }
        })

            */}),
            string1: hereDoc(function(){/*
                <my-com @click.native="handleClick(this)"></my-com>
            */}),
            string2: hereDoc(function(){/*
        <p>总数:{{total}}</p>
        <my-com3 v-model="total"></my-com3>
        <br>
        <button @click="handleReduce">其他地方来的数据,执行-1的操作,同时把改变的值,传到组件中</button>

       Vue.component('my-com3',{
            props:['value'],
            template:'<div><input :value="value" @input="updateValue" id="inp"><label for ="inp">这个是组件</label></div>',
            methods:{
                updateValue(event){
                    this.$emit('input',event.target.value);
                }
            }
        }),
         app 实例 mothods 方法中
         handleReduce(){
                    this.total--;
                },
            */})
            },
            methods:{
                handleGetTotal(total){
                    this.total = total;
                },
                handleClick(e){
                    alert(e)
                },
                handleReduce(){
                    this.total--;
                },
                
               
            }

        })
        
    </script>
    
</body>
</html>

<script>
        //  var as = document.getElementsByTagName('code')
        //                 for(var i=0 ;i<as.length;i++){
        //                     var tar = as[i].innerHTML;
                            
        //                     var res = tar.replace(/</g,'&lt;').replace(/>/g,'&gt;')
                            
                            
        //                     as[i].innerHTML=res;
        //                 }

        
    </script>

